package vistas;

import java.awt.BorderLayout;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.WindowConstants;
import javax.swing.border.EmptyBorder;

import java.awt.Color;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.JLabel;

import contenedorSpring.Contenedor;
import stub.IGestionRemota;
import utilidades.ViewerData;
import core.CorePantalla;

public class FormEscritorioEstudiante extends JFrame {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1774603698584019202L;
	private JPanel contentPane;
	private JLabel lbEscritorio;
	private CorePantalla pantalla;
	private String ip;
	private ViewerData viewer;

	/**
	 * @param pantalla
	 *            the pantalla to set
	 */
	public void setPantalla(CorePantalla pantalla) {
		this.pantalla = pantalla;
	}

	/**
	 * @param viewer
	 *            the viewer to set
	 */
	public void setViewer(ViewerData viewer) {
		this.viewer = viewer;
	}

	/**
	 * @param ip the ip to set
	 */
	public void setIp(String ip) {
		this.ip = ip;
	}

	/**
	 * Create the frame.
	 */
	public FormEscritorioEstudiante() {
		setExtendedState(JFrame.MAXIMIZED_BOTH);
		setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE);
		setBounds(100, 100, 450, 300);

		contentPane = new JPanel();
		contentPane.setBackground(Color.WHITE);
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		contentPane.setLayout(new BorderLayout(0, 0));
		setContentPane(contentPane);

		lbEscritorio = new JLabel("");
		lbEscritorio.addMouseListener(new MouseAdapter() {
			public void mousePressed(MouseEvent e) {
				IGestionRemota remoto = (IGestionRemota) Contenedor.getSingleton().getApplicationContext().getBean("servidorRMI");
				
				remoto.enviarEventoMouse(e, viewer, ip);
			}
		});
		contentPane.add(lbEscritorio, BorderLayout.CENTER);

		addWindowListener(new WindowAdapter() {
			public void windowClosing(WindowEvent we) {
				pantalla.setEstado(false);
				setVisible(false);
				dispose();
			}
		});
	}

	public void iniciarTransmicion() {
		pantalla.setIp(ip);
		pantalla.setEscritorio(lbEscritorio);
		pantalla.setViewer(viewer);
		pantalla.start();
	}

}
